package com.zwz.controller;

import com.zwz.crawler.basic.TungeeBranchOfficeQuery;
import com.zwz.crawler.basic.TungeeEnterpriseInfo;
import com.zwz.crawler.basic.TungeeMemberQuery;
import com.zwz.crawler.mangeInfo.TungeeInvoiceBankQuery;
import com.zwz.crawler.mangeInfo.invertPage.TungeeInvestmentQuery;
import com.zwz.dto.InvestmentEmploymentDTO;
import com.zwz.entity.EnterpriseInfo;
import com.zwz.entity.InvestmentEmploymentInfo;
import com.zwz.service.InvestmentEmploymentService;
import com.zwz.vo.MemInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/investments")
public class InvestmentEmploymentController {

    @Autowired
    private InvestmentEmploymentService service;

    // 创建记录
    @PostMapping
//    public ResponseEntity<?> create(@RequestBody InvestmentEmploymentInfo info) {
        public ResponseEntity<?> create( String enterpriseId ) {
        try {
            // 基础数据校验
//            if (info.getShareholderCount() != null && info.getShareholderCount() < 0) {
//                return ResponseEntity.badRequest().body("股东人数不能为负数");
//            }
//            if (info.getLargestShareholderRatio() != null &&
//                    (info.getLargestShareholderRatio() < 0 || info.getLargestShareholderRatio() > 100)) {
//                return ResponseEntity.badRequest().body("最大股东占比需在0-100之间");
//            }

            MemInfo bean = TungeeMemberQuery.startTungeeMemberQuery(enterpriseId);
            InvestmentEmploymentInfo invert = TungeeBranchOfficeQuery.startTungeeBranchOfficeQuery(enterpriseId);

            int data = TungeeInvestmentQuery.StartTungeeInvestmentQuery(enterpriseId);
            if(data==0){
                invert.setHasForeignInvestment(Boolean.FALSE);
            }
            //reset
            invert.setShareholderCount(Integer.parseInt(bean.getSholdnum()));
            invert.setLargestShareholderRatio(bean.getMaxShold());
            service.create(invert);

            //构建dto 返回
//            InvestmentEmploymentDTO dto=new InvestmentEmploymentDTO(null,invert);
            return ResponseEntity.status(HttpStatus.CREATED).body(invert);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("创建失败: " + e.getMessage());
        }


    }

    // 根据ID查询
    @GetMapping("/{id}")
    public ResponseEntity<?> getById(@PathVariable Integer id) {
        InvestmentEmploymentInfo info = service.getById(id);
        if (info == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(info);
    }

    // 查询全部
    @GetMapping
    public ResponseEntity<List<InvestmentEmploymentInfo>> getAll() {
        return ResponseEntity.ok(service.getAll());
    }

    // 更新记录
    @PutMapping("/{id}")
    public ResponseEntity<?> update(@PathVariable Integer id, @RequestBody InvestmentEmploymentInfo info) {
        try {
            // ID一致性校验
            if (info.getId() != null && !info.getId().equals(id)) {
                return ResponseEntity.badRequest().body("请求体ID与路径ID不一致");
            }
            info.setId(id); // 确保使用路径参数ID

            // 尝试获取原数据校验存在性
            InvestmentEmploymentInfo existing = service.getById(id);
            if (existing == null) {
                return ResponseEntity.notFound().build();
            }

            service.update(info);
            return ResponseEntity.ok(info);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("更新失败: " + e.getMessage());
        }
    }

    // 删除记录
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteById(@PathVariable Integer id) {
        try {
            InvestmentEmploymentInfo existing = service.getById(id);
            if (existing == null) {
                return ResponseEntity.notFound().build();
            }
            service.deleteById(id);
            return ResponseEntity.noContent().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("删除失败: " + e.getMessage());
        }
    }
}